<?php

    // +----------------------------------------------------------------------
    // | 公告管理
    // +----------------------------------------------------------------------
    // | Copyright (c) 2015-2024 http://www.yicmf.com, All rights reserved.
    // +----------------------------------------------------------------------
    // | Author: 微尘 <yicmf@qq.com>
    // +----------------------------------------------------------------------

    namespace app\controller\ucenter\admin;

	use app\controller\admin\Common;
    use app\wechat\model\WechatFans;
    use app\model\ucenter\User as UserModel;
    use app\model\ucenter\ActionLog as ActionLogModel;
    use app\pay\model\UserBalanceCash as UserBalanceCashModel;
    use chart\Chart;

    class Statistics extends Common
    {
        protected function initialize()
        {
            parent::initialize();
            $this->model = new UserModel();
        }

        public function _filter(&$map)
        {
        }

        public function index()
        {
            if ( $this->request->has('data') ) {
                $result = $this->model->where('status', 1)
                    ->field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                //            $chart = new Chart();
                $data = [];
                $data['tooltip'] = ['trigger' => 'axis'];
                $data['legend'] = ['data' => ['注册量']];
                $data['toolbox'] = ['show' => true, 'feature' => [
                    'saveAsImage' => [
                        'show' => true
                    ],
                ]];
                $x = [];
                $series = [];
                for ( $i = 30; $i >= 0; $i-- ) {
                    $day = date('Ymd', strtotime(' -' . $i . ' day'));
                    $num = 0;
                    $x[] = date('m/d', strtotime(' -' . $i . ' day'));;
                    foreach ( $result as $item ) {
                        if ( $item['days'] == $day ) {
                            $num = $item['num'];
                            break;
                        }
                    }
                    $series[] = $num;
                }

                $data['xAxis'] = ['type' => 'category', 'data' => $x];
                $data['yAxis'] = ['type' => 'value'];
                $data['series'] = [
                    ['name' => '注册量', 'type' => 'line', 'data' => $series]
                ];
                return json($data);
            } else {

                // 动态
                $this->assign('action', ActionLogModel::whereTime('create_time', 'd')->count());
                $this->assign('register', UserModel::whereTime('create_time', 'd')->count());
                $this->assign('all', UserModel::whereTime('status', 1)->count());
                //读取规则列表
                return $this->fetch();
            }


        }

        /**
         * 提现
         * @return mixed|\think\response\Json
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/3/27 12:06
         */
        public function cash()
        {
            if ( $this->request->has('data') ) {
                $success = UserBalanceCashModel::where('status', 1)
                    ->field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                $apply = UserBalanceCashModel::field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                $data = [];
                $data['tooltip'] = ['trigger' => 'axis'];
                $data['legend'] = ['data' => ['申请提现', '成功提现']];
                $data['toolbox'] = ['show' => true, 'feature' => [
                    'saveAsImage' => [
                        'show' => true
                    ],
                ]];
                $x = [];
                $series1 = [];
                $series2 = [];
                for ( $i = 30; $i >= 0; $i-- ) {
                    $day = date('Ymd', strtotime(' -' . $i . ' day'));
                    $num = 0;
                    $x[] = date('m/d', strtotime(' -' . $i . ' day'));;
                    foreach ( $apply as $item ) {
                        if ( $item['days'] == $day ) {
                            $num = $item['num'];
                            break;
                        }
                    }
                    $series1[] = $num;
                    $num = 0;
                    foreach ( $success as $item ) {
                        if ( $item['days'] == $day ) {
                            $num = $item['num'];
                            break;
                        }
                    }
                    $series2[] = $num;
                }

                $data['xAxis'] = ['type' => 'category', 'data' => $x];
                $data['yAxis'] = ['type' => 'value'];
                $data['series'] = [
                    ['name' => '申请提现', 'type' => 'bar', 'data' => $series1],
                    ['name' => '成功提现', 'type' => 'bar', 'data' => $series2]
                ];
                return json($data);
            } else {
                $this->assign('today_amount', UserBalanceCashModel::whereTime('create_time', 'd')->sum('amount'));
                $this->assign('today_times', UserBalanceCashModel::whereTime('create_time', 'd')->count());
                $this->assign('need_amount', UserBalanceCashModel::where('status', 0)->sum('amount'));
                $this->assign('need_times', UserBalanceCashModel::where('status', 0)->count());
                $this->assign('accumulate_amount', UserBalanceCashModel::where('status', 1)->sum('amount'));
                $this->assign('accumulate_times', UserBalanceCashModel::where('status', 1)->count());
                //读取规则列表
                return $this->fetch();
            }
        }

        /**
         * 提现
         * @return mixed|\think\response\Json
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/3/27 12:06
         */
        public function consumption()
        {
            if ( $this->request->has('data') ) {
                $success = UserBalanceCashModel::where('status', 1)
                    ->field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                $apply = UserBalanceCashModel::field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                $data = [];
                $data['tooltip'] = ['trigger' => 'axis'];
                $data['legend'] = ['data' => ['申请提现', '成功提现']];
                $data['toolbox'] = ['show' => true, 'feature' => [
                    'saveAsImage' => [
                        'show' => true
                    ],
                ]];
                $x = [];
                $series1 = [];
                $series2 = [];
                for ( $i = 30; $i >= 0; $i-- ) {
                    $day = date('Ymd', strtotime(' -' . $i . ' day'));
                    $num = 0;
                    $x[] = date('m/d', strtotime(' -' . $i . ' day'));;
                    foreach ( $apply as $item ) {
                        if ( $item['days'] == $day ) {
                            $num = $item['num'];
                            break;
                        }
                    }
                    $series1[] = $num;
                    $num = 0;
                    foreach ( $success as $item ) {
                        if ( $item['days'] == $day ) {
                            $num = $item['num'];
                            break;
                        }
                    }
                    $series2[] = $num;
                }

                $data['xAxis'] = ['type' => 'category', 'data' => $x];
                $data['yAxis'] = ['type' => 'value'];
                $data['series'] = [
                    ['name' => '申请提现', 'type' => 'bar', 'data' => $series1],
                    ['name' => '成功提现', 'type' => 'bar', 'data' => $series2]
                ];
                return json($data);
            } else {
                $this->assign('today_amount', UserBalanceCashModel::whereTime('create_time', 'd')->sum('amount'));
                $this->assign('today_times', UserBalanceCashModel::whereTime('create_time', 'd')->count());
                $this->assign('need_amount', UserBalanceCashModel::where('status', 0)->sum('amount'));
                $this->assign('need_times', UserBalanceCashModel::where('status', 0)->count());
                $this->assign('accumulate_amount', UserBalanceCashModel::where('status', 1)->sum('amount'));
                $this->assign('accumulate_times', UserBalanceCashModel::where('status', 1)->count());
                //读取规则列表
                return $this->fetch();
            }
        }

        /**
         * 活跃
         * @return mixed|\think\response\Json
         * @throws \think\db\exception\DataNotFoundException
         * @throws \think\db\exception\ModelNotFoundException
         * @throws \think\exception\DbException
         * @author  : 微尘 <yicmf@qq.com>
         * @datetime: 2019/3/27 12:06
         */
        public function active()
        {
            if ( $this->request->has('data') ) {
                $result = $this->model->where('status', 1)
                    ->field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                $data = [];
                $data['tooltip'] = ['trigger' => 'axis'];
                $data['legend'] = ['data' => ['注册量']];
                $data['toolbox'] = ['show' => true, 'feature' => [
                    'saveAsImage' => [
                        'show' => true
                    ],
                ]];
                $x = [];
                $series = [];
                for ( $i = 30; $i >= 0; $i-- ) {
                    $day = date('Ymd', strtotime(' -' . $i . ' day'));
                    $num = 0;
                    $x[] = date('m/d', strtotime(' -' . $i . ' day'));;
                    foreach ( $result as $item ) {
                        if ( $item['days'] == $day ) {
                            $num = $item['num'];
                            break;
                        }
                    }
                    $series[] = $num;
                }

                $data['xAxis'] = ['type' => 'category', 'data' => $x];
                $data['yAxis'] = ['type' => 'value'];
                $data['series'] = [
                    ['name' => '注册量', 'type' => 'line', 'data' => $series]
                ];
                return json($data);
            } else {
                $result = ActionLogModel::where('status', 1)
                    ->field('DATE_FORMAT(create_time,"%Y%m%d") days,count(id) num')->whereTime('create_time', ' -30 day')->group('days')->select();
                // 活跃
                $this->assign('active', ActionLogModel::whereTime('create_time', 'd')->count());
                $this->assign('highest', UserModel::whereTime('create_time', 'd')->count());
                $this->assign('all', UserModel::whereTime('status', 1)->count());
                //读取规则列表
                return $this->fetch();
            }
        }

    }
